#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
int main(){
  printf("before I father pid=%d,ppid=%d\n",getpid(),getppid());
  putenv("AAAA=1000");
  pid_t id =fork();
  char *const argv[] ={"ls","-a","-l",NULL};
  extern char ** environ;
  char *const tmp[]={"aaa=111","bbb=222",NULL};
  if(id==0){

  printf("I child pid=%d,ppid=%d\n",getpid(),getppid());
  //execl("/usr/bin/ls","ls","-a","-l",NULL);
  //execlp("ls","ls","-a","-l",NULL);
 // execv("/usr/bin/ls",argv);
  //execl("./mytest1","mytest1","-a","-b","-c",NULL);
  execle("./mytest1","mytest1","-a","-b","-c",NULL,environ);
  //execle("./mytest1","mytest1","-a","-b","-c",NULL,tmp);
  printf("after I child pid=%d,ppid=%d\n",getpid(),getppid());
  exit(0);
  }
  int status = 0;
  while(1){
   pid_t ret = waitpid(id,&status,WNOHANG); 
  if(ret>0){
    if(WIFEXITED(status)){
    printf("wait success,exit code:%d\n",WEXITSTATUS(status));//返回进程退出码
    printf("after I pid=%d,ret =%d\n",getpid(),ret);
    break;
    }
    else{//进程异常结束                                                                                 
    printf("wait success,error code:%d\n",WTERMSIG(status));//返回进程信号编号
    break;
    }
  }
  else if(ret < 0){
    printf("wait fail");
    break;
  }
  }
  usleep(50000);
}
